﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.DataAccess;
using QLTV_V2.BussinessLogic;
using QLTV_V2.BussinessObject;

namespace QLTV_V2
{
    public partial class FrmTaoMaCB : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmTaoMaCB()
        {
            InitializeComponent();
        }
        public string madausach, mact;
        Utilities uti = new Utilities();
        SachBUS sachBUS = new SachBUS();
        DauSach ds = new DauSach();
        BanSach bs = new BanSach();
        private void FrmTaoMaCB_Load(object sender, EventArgs e)
        {
            comb_ViTri.DataSource = uti.getDataSource("ViTri", "MaViTri");
            comb_ViTri.DisplayMember = "MaViTri";
            comb_ViTri.ValueMember = "MaViTri";
            //Load du lieu
            ds = sachBUS.getDauSach_object("s", "s", madausach, 2);
            txt_MaKho.Text = ds.MaLoaiSach;
            txt_MaDauSach.Text = madausach;
            txt_Soluong.Text = ds.SoLuongTam.ToString();
            comb_ViTri.SelectedValue = ds.ViTriTam;
            // 
            MessageBox.Show(mact);
        }

        private void btn_Tao_Click(object sender, EventArgs e)
        {
            int soluong = Convert.ToInt32(txt_Soluong.Text);
            int kt = 0;
            for (int i = 0; i < soluong; i++)
            {
                bs = getBanSach_object();
                if (sachBUS.insertBanSach(bs) == true)
                    kt++;                
            }
            if (kt != 0)
            {
                if(sachBUS.update_After_BM_DK(mact,2)==true)
                MessageBox.Show("Tạo thành công " + kt + " mã ca biệt");
            }
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private BanSach getBanSach_object()
        {
            string lastID = uti.GetLastID_DK("BanSach", "MaDauSach", ds.MaDauSach, "MaBanSach", "MaBanSach", ds.MaDauSach + "-0000");
            string nextID = uti.GetNextID(lastID, ds.MaDauSach + "-");
            bs.MaBanSach = nextID.ToUpper();
            bs.MaDauSach = txt_MaDauSach.Text.ToUpper();
            bs.NgayCN = System.DateTime.Now;
            bs.MaViTri = comb_ViTri.SelectedValue.ToString();
            bs.TinhTrang = "Tot";
            bs.TrangThai = "San sang";
            return bs;
        }
    }
}
